﻿using BLL.BatchBLL;
using Model;
using System;
using System.Windows.Forms;

namespace MDMUI
{
    public partial class BatchCreationForm : Form
    {
        private int workOrderId;
        public Batch CreatedBatch { get; private set; }
        private TextBox txtTotalProcessId;
        private ComboBox cmbBatchType;
        private TextBox txtQuantity;
        private TextBox txtSubQuantity;
        private TextBox txtGoodQuantity;
        private TextBox txtNGQuantity;
        private TextBox txtUnit;
        private TextBox txtLockStatus;
        private TextBox txtEventName;
        private TextBox txtEventUser;
        private DateTimePicker dtpEventTime;
        private TextBox txtEventRemarks;

        private createBatchBLL batchBLL = new createBatchBLL();

        public BatchCreationForm(int workOrderId)
        {
            InitializeComponent();
            this.workOrderId = workOrderId;
            InitializeForm();
        }

        private void InitializeForm()
        {
            this.Text = "创建批次";
            this.Size = new Size(400, 600);
            this.StartPosition = FormStartPosition.CenterScreen;

            CreateControls();

            Button btnOK = new Button { Text = "确定", Left = 50, Top = 450, Width = 80, DialogResult = DialogResult.OK };
            Button btnCancel = new Button { Text = "取消", Left = 200, Top = 450, Width = 80, DialogResult = DialogResult.Cancel };

            btnOK.Click += (s, e) => ValidateAndSave();
            btnCancel.Click += (s, e) => this.Close();

            this.Controls.Add(btnOK);
            this.Controls.Add(btnCancel);

            // 动态获取工艺流程号
            int newBatchId = batchBLL.GetMaxBatchId() + 1;
            Label lblBatchId = new Label { Text = "批次号", Left = 50, Top = 10 };
            TextBox txtBatchId = new TextBox { Left = 150, Top = 10, Enabled = false };
            txtBatchId.Text = newBatchId.ToString();
            this.Controls.Add(lblBatchId);
            this.Controls.Add(txtBatchId);

            // 动态获取工艺流程号并填充到文本框
            var product = batchBLL.GetProductByWorkOrderId(workOrderId);
            if (product != null)
            {
                txtTotalProcessId.Text = product.FlowId.ToString();
                txtTotalProcessId.ReadOnly = true; // 设置为只读
            }
        }

        private void CreateControls()
        {
            int controlTop = 40;
            int spacing = 30;

            Label lblWorkOrderId = new Label { Text = "工单号", Left = 50, Top = controlTop };
            TextBox txtWorkOrderId = new TextBox { Left = 150, Top = controlTop, Enabled = false };
            txtWorkOrderId.Text = workOrderId.ToString();
            this.Controls.Add(lblWorkOrderId);
            this.Controls.Add(txtWorkOrderId);
            controlTop += spacing;

            Label lblTotalProcessId = new Label { Text = "总流程号", Left = 50, Top = controlTop };
            txtTotalProcessId = new TextBox { Left = 150, Top = controlTop };
            this.Controls.Add(lblTotalProcessId);
            this.Controls.Add(txtTotalProcessId);
            controlTop += spacing;

            Label lblBatchType = new Label { Text = "批次类型", Left = 50, Top = controlTop };
            cmbBatchType = new ComboBox { Left = 150, Top = controlTop };
            cmbBatchType.Items.AddRange(new[] { "finishProduct", "otherType1", "otherType2" });
            this.Controls.Add(lblBatchType);
            this.Controls.Add(cmbBatchType);
            controlTop += spacing;

            Label lblQuantity = new Label { Text = "数量", Left = 50, Top = controlTop };
            txtQuantity = new TextBox { Left = 150, Top = controlTop };
            this.Controls.Add(lblQuantity);
            this.Controls.Add(txtQuantity);
            controlTop += spacing;

            Label lblSubQuantity = new Label { Text = "子数量", Left = 50, Top = controlTop };
            txtSubQuantity = new TextBox { Left = 150, Top = controlTop };
            this.Controls.Add(lblSubQuantity);
            this.Controls.Add(txtSubQuantity);
            controlTop += spacing;

            Label lblGoodQuantity = new Label { Text = "Good数量", Left = 50, Top = controlTop };
            txtGoodQuantity = new TextBox { Left = 150, Top = controlTop };
            this.Controls.Add(lblGoodQuantity);
            this.Controls.Add(txtGoodQuantity);
            controlTop += spacing;

            Label lblNGQuantity = new Label { Text = "NG数量", Left = 50, Top = controlTop };
            txtNGQuantity = new TextBox { Left = 150, Top = controlTop };
            this.Controls.Add(lblNGQuantity);
            this.Controls.Add(txtNGQuantity);
            controlTop += spacing;

            Label lblUnit = new Label { Text = "单位", Left = 50, Top = controlTop };
            txtUnit = new TextBox { Left = 150, Top = controlTop };
            this.Controls.Add(lblUnit);
            this.Controls.Add(txtUnit);
            controlTop += spacing;

            Label lblLockStatus = new Label { Text = "锁定状态", Left = 50, Top = controlTop };
            txtLockStatus = new TextBox { Left = 150, Top = controlTop, ReadOnly = true };
            txtLockStatus.Text = "未投产";
            this.Controls.Add(lblLockStatus);
            this.Controls.Add(txtLockStatus);
            controlTop += spacing;

            Label lblEventName = new Label { Text = "事件名称", Left = 50, Top = controlTop };
            txtEventName = new TextBox { Left = 150, Top = controlTop };
            this.Controls.Add(lblEventName);
            this.Controls.Add(txtEventName);
            controlTop += spacing;

            Label lblEventUser = new Label { Text = "事件用户", Left = 50, Top = controlTop };
            txtEventUser = new TextBox { Left = 150, Top = controlTop };
            this.Controls.Add(lblEventUser);
            this.Controls.Add(txtEventUser);
            controlTop += spacing;

            Label lblEventTime = new Label { Text = "事件时间", Left = 50, Top = controlTop };
            dtpEventTime = new DateTimePicker { Left = 150, Top = controlTop };
            this.Controls.Add(lblEventTime);
            this.Controls.Add(dtpEventTime);
            controlTop += spacing;

            Label lblEventRemark = new Label { Text = "事件备注", Left = 50, Top = controlTop };
            txtEventRemarks = new TextBox { Left = 150, Top = controlTop };
            this.Controls.Add(lblEventRemark);
            this.Controls.Add(txtEventRemarks);
            controlTop += spacing;
        }

        private void ValidateAndSave()
        {
            try
            {
                CreatedBatch = new Batch
                {
                    WorkOrderId = workOrderId,
                    TotalProcessId = int.Parse(txtTotalProcessId.Text),
                    BatchType = cmbBatchType.SelectedItem.ToString(),
                    Quantity = int.Parse(txtQuantity.Text),
                    SubQuantity = int.Parse(txtSubQuantity.Text),
                    GoodQuantity = int.Parse(txtGoodQuantity.Text),
                    NGQuantity = int.Parse(txtNGQuantity.Text),
                    Unit = txtUnit.Text,
                    LockStatus = txtLockStatus.Text,
                    EventName = txtEventName.Text,
                    EventUser = txtEventUser.Text,
                    EventTime = dtpEventTime.Value,
                    EventRemarks = txtEventRemarks.Text
                };

                CreatedBatch.BatchId = batchBLL.GetMaxBatchId() + 1;

                batchBLL.SaveBatch(CreatedBatch);

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"数据验证失败：{ex.Message}");
            }
        }
    }
}